{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "Python 3.7.1 64-bit ('base': conda)",
   "display_name": "Python 3.7.1 64-bit ('base': conda)",
   "metadata": {
    "interpreter": {
     "hash": "2266c607543d224cb119288ea55888d6fda87cc9a4c78c02ed099d39082a76ce"
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Wall time: 4.99 ms\nWall time: 2.47 s\nWall time: 0 ns\nWall time: 82.1 ms\n"
    }
   ],
   "source": [
    "%time from pdb_profiling import default_config\n",
    "%time from pdb_profiling.processors import Identifier, UniProtFASTA\n",
    "%time from pdb_profiling.utils import a_seq_reader\n",
    "\n",
    "%time default_config('C:/GitWorks/pdb-profiling/test/demo')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<RefSeq protein NP_001291289 1>"
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "demo = Identifier('NP_001291289.1')\n",
    "demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "('Q9C0B2', None)"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "entry, isoform = demo.map2unp().result()\n",
    "entry, isoform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "{'protein': 'NP_001291289.1', 'transcript': 'NM_001304360.1', 'gene': None}"
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "demo.get_all_level_identifiers().result()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "('>NP_001291289.1 cilia- and flagella-associated protein 74 [Homo sapiens]',\n 'MEDDGSLLPEDELLADALLLEDERDELEDPEFDIKCLLQEAEDDVDPGHSSSVKELDTDADKLKKKTAEDRTQAFHLRQNLSALDKMHEEQELFTEKMRGELRACRQRRDLIDKQQEAVAAEIATEEEAGNMAAVGRLQAVSRRLFAELENERDLQSRTEAVLKESENTMWHIEIQEGRLEAFRTADREEVEATGRRLQVRAAEQLCREQEALGKVERNRLLRIRKSLNTQKELGLRHQKLLEDARKNHKVAVRFLKASLGRIREQEKKEEMECHEYMRRRMDAVVALKGSISANRDTLRKFQAWDRAKAELAEQRVQAEKKAILAQGRDAFRHLVHQRRRQELEAQKRAFEEEQKLRKQEIISRILKEEAEEEKRKKQHPPTSARHRLTLRDKTWNYISDFCKKTTVPTNTYTLDYEAAAGPGPSRLLEVVSSELIQGDPGASSEEETLAEPEISGLWNEDYKPYQVPKEDVDRKPVGGTKMDKDILERTVERLRSRVVHKQVVWGREFQGRPFNSKPELLHFQDFDIGKVYKKKITLVNTTYTINYCKLVGVEEHLRDFIHVDFDPPGPLSAGMSCEVLVTFKPMINKDLEGNISFLAQTGEFSVPLKCSTKKCSLSLDKELIDFGSYVVGETTSRTITLTNVGGLGTTFKFLPASEPCEMDDSQSALKLSSLLTYEDKSLYDKAATSFSEQQLEGTESSQADMQSRKELEKLDKEQEEEQPAEPERLTTVIPPSEEQTEITLGEVTEGEIGPFSSIKVPIVFTPVVPGDVQARFKVTFKNPQCPTLHFRVVGVAIDVPVWVPKPSVDLKICMYDRLYQDSVLVHTRSKAALRLKFEVCKELRAHLELLPKTGYIQAQSSYSVQLKFLPRHSLPEDAGRYFDKETRVLEAPMTIWVADQNKPVGFTVHAIVTTSDLELSPSEVDFGYCTIYEAIRTEISLHNHSLLPQEFGFVRLPKFVDVQPNDGFGTILPLETLQFCVIFQPTKAEEHRFQLTCKSEINRCFKLSCRAVGVHPPLELSHYQIKFAATALYDTSVATVYVINSHLSMSSPTHSKPRIGSEDASPMGPTSFEFLLPPDSPITISPSVGTVWPGKRCLVQVAFRPVLPEKLIRQEALPLLNKEMETKSFRKNMAPQRKDLHGLSFSVLRAQNRDKLFKVSVPHVLEMRKRELRPSSDEYQAARATLLRAFQAKFDTFVVPCVVASGDIKDRKGSEPLSFSPHNTLYLELWCPTVAPSVVVTSHKGKTIFNFGDVAVGHRSIKKISIQNVSPEDLALDFSLLNPNGPFVLLNHSSLLRAGGTQVLVLSFSPHESILAQETLDIITKRGTLTLTLMGTGVASMITCSIEGSVLNMGYVIAGESVSSGFKLQNNSLLPIKFSMHLDSLSSTRGRGQQQLPQFLSSPSQRTEVVGTQNLNGQSVFSVAPVKGVMDPGKTQDFTVTFSPDHESLYFSDKLQVVLFEKKISHQILLKGAACQHMMFVEGGDPLDVPVESLTAIPVFDPRHREASSRPGPLSPEAEELRPILVTLDYIQFDTDTPAPPATRELQVGCIRTTQPSPKKTVEFSIDSVASLQHKGFSIEPSRGSVERGQTKTISISWVPPADFDPDHPLMVSALLQLRGDVKETYKVIFVAQVLTGP')"
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "'''\n",
    "Identifier(\n",
    "    demo.get_all_level_identifiers().result()['protein']\n",
    ").fetch_sequence().result()\n",
    "'''\n",
    "np_header, np_seq = demo.fetch_sequence().result()\n",
    "np_header, np_seq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "True"
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "source": [
    "demo.status"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "('>sp|Q9C0B2|CFA74_HUMAN Cilia- and flagella-associated protein 74 OS=Homo sapiens OX=9606 GN=CFAP74 PE=2 SV=3',\n 'MEDDGSLLPEDELLADALLLEDERDELEDPEFDIKCLLQEAEDDVDPGHSSSVKELDTDADKLKKKTAEDRTQAFHLRQNLSALDKMHEEQELFTEKMRGELRACRQRRDLIDKQQEAVAAEIATEEEAGNMAAVGRLQAVSRRLFAELENERDLQSRTEAVLKESENTMWHIEIQEGRLEAFRTADREEVEATGRRLQVRAAEQLCREQEALGKVERNRLLRIRKSLNTQKELGLRHQKLLEDARKNHKVAVRFLKASLGRIREQEKKEEMECHEYMRRRMDAVVALKGSISANRDTLRKFQAWDRAKAELAEQRVQAEKKAILAQGRDAFRHLVHQRRRQELEAQKRAFEEEQKLRKQEIISRILKEEAEEEKRKKQHPPTSARHRLTLRDKTWNYISDFCKKTTVPTNTYTLDYEAAAGPGPSRLLEVVSSELIQGDPGASSEEETLAEPEISGLWNEDYKPYQVPKEDVDRKPVGGTKMDKDILERTVERLRSRVVHKQVVWGREFQGRPFNSKPELLHFQDFDIGKVYKKKITLVNTTYTINYCKLVGVEEHLRDFIHVDFDPPGPLSAGMSCEVLVTFKPMINKDLEGNISFLAQTGEFSVPLKCSTKKCSLSLDKELIDFGSYVVGETTSRTITLTNVGGLGTTFKFLPASEPCEMDDSQSALKLSSLLTYEDKSLYDKAATSFSEQQLEGTESSQADMQSRKELEKLDKEQEEEQPAEPERLTTVIPPSEEQTEITLGEVTEGEIGPFSSIKVPIVFTPVVPGDVQARFKVTFKNPQCPTLHFRVVGVAIDVPVWVPKPSVDLKICMYDRLYQDSVLVHTRSKAALRLKFEVCKELRAHLELLPKTGYIQAQSSYSVQLKFLPRHSLPEDAGRYFDKETRVLEAPMTIWVADQNKPVGFTVHAIVTTSDLELSPSEVDFGYCTIYEAIRTEISLHNHSLLPQEFGFVRLPKFVDVQPNDGFGTILPLETLQFCVIFQPTKAEEHRFQLTCKSEINRCFKLSCRAVGVHPPLELSHYQIKFAATALYDTSVATVYVINSHLSMSSPTHSKPRIGSEDASPMGPTSFEFLLPPDSPITISPSVGTVWPGKRCLVQVAFRPVLPEKLIRQEALPLLNKEMETKSFRKNMAPQRKDLHGLSFSVLRAQNRDKLFKVSVPHVLEMRKRELRPSSDEYQAARATLLRAFQAKFDTFVVPCVVASGDIKDRKGSEPLSFSPHNTLYLELWCPTVAPSVVVTSHKGKTIFNFGDVAVGHRSIKKISIQNVSPEDLALDFSLLNPNGPFVLLNHSSLLRAGGTQVLVLSFSPHESILAQETLDIITKRGTLTLTLMGTGVASMITCSIEGSVLNMGYVIAGESVSSGFKLQNNSLLPIKFSMHLDSLSSTRGRGQQQLPQFLSSPSQRTEVVGTQNLNGQSVFSVAPVKGVMDPGKTQDFTVTFSPDHESLYFSDKLQVVLFEKKISHQILLKGAACQHMMFVEGGDPLDVPVESLTAIPVFDPRHREEAEELRPILVTLDYIQFDTDTPAPPATRELQVGCIRTTQPSPKKPDHPLMVSALLQLRGDVKETYKVIFVAQVLTGP')"
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "source": [
    "unp_header, unp_seq = UniProtFASTA.single_retrieve(entry, UniProtFASTA.folder, UniProtFASTA.web_semaphore).then(a_seq_reader).result()\n",
    "unp_header, unp_seq"
   ]
  },
  {
   "source": [
    "```py\n",
    "from dtaidistance import alignment\n",
    "%time value, matrix = alignment.needleman_wunsch(unp_seq, np_seq)\n",
    "algn, s1a, s2a = alignment.best_alignment(matrix, unp_seq, np_seq, gap='-')\n",
    "print(''.join(s1a[1500:]))\n",
    "print(''.join(s2a[1500:]))\n",
    "\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "plt.style.use('ggplot')\n",
    "plt.figure(figsize=(10,8))\n",
    "sns.heatmap(matrix[1500:,1500:],cmap='icefire')\n",
    "plt.show()\n",
    "```\n",
    "\n",
    "<img src=\"../docs/figs/ToUniProt_ali.svg\">"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Wall time: 0 ns\nWall time: 20.9 ms\nWall time: 0 ns\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(((0, 1507), (1507, 1551), (1551, 1552), (1552, 1553), (1553, 1584)),\n ((0, 1507), (1517, 1561), (1581, 1582), (1603, 1604), (1608, 1639)))"
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "from Bio import Align\n",
    "%time aligner = Align.PairwiseAligner()\n",
    "%time alignments = aligner.align(unp_seq, np_seq)\n",
    "%time alignments[0].aligned"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "FDPRHRE----------EAEELRPILVTLDYIQFDTDTPAPPATRELQVGCIRTTQPSPKK--------------------P---------------------D----HPLMVSALLQLRGDVKETYKVIFVAQVLTGP\n|||||||----------||||||||||||||||||||||||||||||||||||||||||||--------------------|---------------------|----|||||||||||||||||||||||||||||||\nFDPRHREASSRPGPLSPEAEELRPILVTLDYIQFDTDTPAPPATRELQVGCIRTTQPSPKKTVEFSIDSVASLQHKGFSIEPSRGSVERGQTKTISISWVPPADFDPDHPLMVSALLQLRGDVKETYKVIFVAQVLTGP\n\n"
    }
   ],
   "source": [
    "for i in str(alignments[0]).split('\\n'):\n",
    "    print(i[1500:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}